package com.mmloo.platform.controller;

import com.mmloo.common.message.Message;
import com.mmloo.common.model.Goods;
import com.mmloo.manage.service.GoodsManageService;
import com.mmloo.platform.model.ShoppingCart;
import com.mmloo.platform.service.BusinessService;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;

/**
 * Created by jessy on 2015/10/21.
 * 商品业务处理控制器
 * 商品详情
       查看商品详情
      直接购买
     分享
 */
@Controller
@RequestMapping("/business/")
public class BusinessController {
    private Logger log = LoggerFactory.getLogger(BusinessController.class);

    @Autowired
    GoodsManageService goodsManageService;

    @Autowired
    BusinessService businessService;

    /**
     * 获取指定id的商品详细信息
     * 点击 click_count++
     * 人气指数 hot *5 ++
     * @param id  商品id
     * @return
     */
    @ResponseBody
    @RequestMapping(value = "goodsDetail",method = RequestMethod.GET)
    public String getGoodsDetail(@RequestParam(value = "id",required = true)String id){
        String message = "商品详细信息获取成功";
        try {
            Goods goods = goodsManageService.findGoods(id);
            goods.setClick_count(goods.getClick_count()+1);
            goods.setHot(goods.getHot()+5);
            goodsManageService.update(goods);
            log.debug("获取商品 id 为 " + id + "的详细信息");
            return new Message(true,message,goods).toJson();
        } catch (Exception e) {
            message = "商品详细信息获取失败";
            return new Message(false,message).toJson();
        }
    }

    /**
     * 选择商品添加进购物车
     * @param loginUserId
     * @param shoppingId
     * @return
     */
    @ResponseBody
    @RequestMapping(value = "selectInCart",method = RequestMethod.POST)
    public String selectInCart(@RequestParam(value = "loginUserId",required = true)String loginUserId,
                               @RequestParam(value = "shoppingId",required = true)String shoppingId){
        try {
            ShoppingCart shoppingCart = businessService.findCartByOwner(loginUserId);
            if(shoppingCart != null){
                StringBuilder selected_goods = new StringBuilder(shoppingCart.getSelected_goods()).append(",").append(shoppingId);
                shoppingCart.setSelected_goods(selected_goods.toString());
                shoppingCart.setState(2);
                businessService.update(shoppingCart);
            }else{
                shoppingCart = new ShoppingCart();
                shoppingCart.setOwner(loginUserId);
                shoppingCart.setSelected_goods(shoppingId);
                shoppingCart.setState(2);
                businessService.addCart(shoppingCart);
            }
            return new Message(true,"商品添加到购物车添加成功",shoppingCart).toJson();
        } catch (Exception e) {
            return new Message(false,"商品添加到购物车添加失败").toJson();

        }
    }

    /**
     * 商品分享成功 添加人气指数
     * 每分享一次 人气指数1*20增加
     * @param id
     * @return
     */
    @ResponseBody
    @RequestMapping(value = "share",method = RequestMethod.GET)
    public String shareGoods(@RequestParam(value = "id",required = true)String id){
        Goods goods = null;
        try {
            goods = goodsManageService.findGoods(id);
            goods.setHot(goods.getHot() + 20);
            goodsManageService.update(goods);
            return new Message(true,"分享成功").toJson();
        } catch (Exception e) {
            e.printStackTrace();
        }
        return new Message(false,"分享失败").toJson();
    }


}
